package com.jamiexu.cn.servlet;

import com.jamiexu.cn.music.cloud.CloudMusic;
import com.jamiexu.cn.music.cloud.CloudMusicBean;
import com.jamiexu.cn.music.kugou.KGBean;
import com.jamiexu.cn.music.kugou.KGMusic;
import com.jamiexu.cn.music.kuwo.KWBean;
import com.jamiexu.cn.music.kuwo.KWMusic;
import com.jamiexu.cn.music.qq.QQMusic;
import com.jamiexu.cn.music.qq.QQMusicBean;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

//@WebServlet("/music")
public class Music extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String type = req.getParameter("type");
        String keyword = req.getParameter("keyword");
        int page = req.getParameter("page") == null ? 1 : Integer.parseInt(req.getParameter("page"));
        JSONObject jsonObject = new JSONObject();

        if (type == null || type.length() == 0
                || keyword == null || keyword.length() == 0) {
            try {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "faild");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            resp.getWriter().println(jsonObject.toString());
            return;
        }
        try {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "successful");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JSONArray jsonArray = new JSONArray();
        switch (type) {
            case "qq":
                QQMusic qqMusic = new QQMusic();
                qqMusic.loadSong(keyword, page);
                ArrayList<QQMusicBean> qqMusicBeans = qqMusic.getDataLists();
                for (QQMusicBean qqMusicBean : qqMusicBeans) {
                    try {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("name", qqMusicBean.getName());
                        jsonObject1.put("singe_name", qqMusicBean.getSingerName());
                        jsonObject1.put("play_url", qqMusicBean.getUrl());
                        jsonObject1.put("file_size", qqMusicBean.getSize());
                        jsonArray.put(jsonObject1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    jsonObject.put("lists", jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resp.getWriter().print(jsonObject);
                break;

            case "cloud":
                CloudMusic cloudMusic = new CloudMusic();
                cloudMusic.loadSong(keyword, page - 1);
                ArrayList<CloudMusicBean> cloudMusicBeans = cloudMusic.getDataLists();
                for (CloudMusicBean cloudMusicBean : cloudMusicBeans) {
                    try {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("name", cloudMusicBean.getName());
                        jsonObject1.put("singe_name", cloudMusicBean.getSingerName());
                        jsonObject1.put("play_url", cloudMusicBean.getUrl());
                        jsonObject1.put("file_size", cloudMusicBean.getSize());
                        jsonArray.put(jsonObject1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    jsonObject.put("lists", jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resp.getWriter().print(jsonObject);
                break;

            case "kw":
                KWMusic kwMusic = new KWMusic();
                kwMusic.loadSong(keyword, page);
                ArrayList<KWBean> kwBeans = kwMusic.getDataLists();
                for (KWBean kwBean : kwBeans) {
                    try {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("name", kwBean.getName());
                        jsonObject1.put("singe_name", kwBean.getSingerName());
                        jsonObject1.put("play_url", kwBean.getUrl());
                        jsonObject1.put("file_size", kwBean.getSize());
                        jsonArray.put(jsonObject1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    jsonObject.put("lists", jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resp.getWriter().print(jsonObject);
                break;


            case "kg":
                KGMusic kgMusic = new KGMusic();
                kgMusic.loadSong(keyword, page);
                ArrayList<KGBean> kgBeans = kgMusic.getDataLists();
                for (KGBean kgBean : kgBeans) {
                    try {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("name", kgBean.getName());
                        jsonObject1.put("singe_name", kgBean.getSingerName());
                        jsonObject1.put("play_url", kgBean.getUrl());
                        jsonObject1.put("file_size", kgBean.getSize());
                        jsonArray.put(jsonObject1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    jsonObject.put("lists", jsonArray);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resp.getWriter().print(jsonObject);
                break;

        }
    }
}
